package com.xiaobaibai.advice;

import com.xiaobaibai.myExcetion.SecurityMyException;
import com.xiaobaibai.response.ResponseCode;
import com.xiaobaibai.response.ResponseFormat;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalAdvice {

    /**
     * 传参错误
     */
    @ExceptionHandler( { MethodArgumentNotValidException.class})
    public ResponseFormat missingServletRequestParameterException(MethodArgumentNotValidException e){
        return ResponseFormat.failure(e.getBindingResult()
                .getAllErrors()
                .get(0)
                .getDefaultMessage());//返回第一个
    }

    @ExceptionHandler( SecurityMyException.class)
    public ResponseFormat myException(RuntimeException e){
        e.printStackTrace();
        return ResponseFormat.failure("服务器错误,请联系管理员");
    }

    @ExceptionHandler( Exception.class)
    public ResponseFormat exception(Exception e){
        e.printStackTrace();
        return ResponseFormat.failure("服务器繁忙");
    }

}
